package com.yzkj.cpjxc.business.service.impl;

import com.yzkj.cpjxc.business.framework.common.BusinessMap;
import com.yzkj.cpjxc.business.framework.common.Pager;
import com.yzkj.cpjxc.business.framework.utils.IDUtils;
import com.yzkj.cpjxc.business.domain.SysMethodCallTime;
import com.yzkj.cpjxc.business.domain.SysMethodCallTimeQueryVo;
import com.yzkj.cpjxc.business.service.SysMethodCallTimeService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 方法调用时间表 Service 实现
 */
@Service
public class SysMethodCallTimeServiceImpl implements SysMethodCallTimeService {

	/**
	 * MyBatis 命名空间
	 */
	private final static String MAPPER_NAMESPACE = "com.chkj.sysMgr.monitor.sysMethodCallTime.mappers.SysMethodCallTimeMapper";

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	@Override
	public BusinessMap create(SysMethodCallTime smc) throws Exception {
		BusinessMap bm = new BusinessMap();
		if (StringUtils.isBlank(smc.getUuid())) {
			smc.setUuid(IDUtils.genUUID());
		}
		sqlSessionTemplate.insert(MAPPER_NAMESPACE + ".add", smc);
		return bm;
	}

	@Override
	public boolean isExists(SysMethodCallTime smc) throws Exception {
		return (Integer)sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".isExists", smc) > 0;
	}
	
	@Override
	public SysMethodCallTime getById(String sysMethodCallTimeId) throws Exception {
		return sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getById", sysMethodCallTimeId);
	}
	
	@Override
	public BusinessMap edit(SysMethodCallTime smc) throws Exception {
		BusinessMap bm = new BusinessMap();
		sqlSessionTemplate.update(MAPPER_NAMESPACE + ".edit", smc);
		return bm;
	}

	@Override
	public boolean delete(String sysMethodCallTimeId) throws Exception {
		sqlSessionTemplate.delete(MAPPER_NAMESPACE + ".deleteById", sysMethodCallTimeId);
		return true;
	}

	@Override
	public Pager<SysMethodCallTime> search(Pager<SysMethodCallTime> pager, SysMethodCallTimeQueryVo queryVo) throws Exception {
		List dataList = sqlSessionTemplate.selectList(MAPPER_NAMESPACE + ".page", queryVo, new RowBounds((pager.getCurrentPage() - 1) * pager.getLimit(), pager.getLimit()));
		pager.setDataList(dataList);
		
		// 如果需要转换成ShowVo展示，那么就在这里转换
		
		Long total = sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getTotal", queryVo);
		pager.setTotal(total);
		return pager;
	}

}
